package net.root.main;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.Font;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimerTask;

import javax.swing.JButton;
import net.root.player.WavPlayer;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MainSwing {
	private JFrame frame;
	JLabel label_2;
	JLabel label_3;
	JComboBox<String> comboBox;
	JComboBox<String> comboBox_1;
	JComboBox<String> comboBox_2;
	JComboBox<String> comboBox_3;
	java.util.Timer jobScheduler = new java.util.Timer();
	long startTime;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainSwing window = new MainSwing();
					window.frame.setVisible(true);
					NowTime nt = new NowTime(window);
					Thread ntt = new Thread(nt);
					ntt.start();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MainSwing() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	@SuppressWarnings({})
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 280, 350);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JLabel lblNewLabel = new JLabel("전체교시설정");
		lblNewLabel.setFont(new Font("굴림", Font.PLAIN, 14));

		JLabel lblNewLabel_1 = new JLabel("교시시간설정");
		lblNewLabel_1.setFont(new Font("굴림", Font.PLAIN, 14));

		JLabel label = new JLabel("쉬는시간설정");
		label.setFont(new Font("굴림", Font.PLAIN, 14));

		JLabel label_1 = new JLabel("점심시간설정");
		label_1.setFont(new Font("굴림", Font.PLAIN, 14));

		label_2 = new JLabel("타이머시간 : 00:00:00");
		label_2.setFont(new Font("굴림", Font.PLAIN, 18));

		label_3 = new JLabel("현재시간 : 00:00:00 AM");
		label_3.setFont(new Font("굴림", Font.PLAIN, 18));

		comboBox = new JComboBox<String>();
		for (int i = 1; i < 11; i++) {
			comboBox.addItem(i + "교시");
		}
		comboBox.setSelectedIndex(7);
		comboBox.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent arg0) {
				// TODO Auto-generated method stub
				int select = Integer.parseInt(((String) arg0.getItem())
						.substring(0, 1));
				try {
					comboBox_3.removeAllItems();
				} catch (Exception e) {}
				comboBox_3.addItem("없음");
				for (int i = 1; i <= select; i++) {
					comboBox_3.addItem(i + "교시 이후");
				}
			}
		});

		comboBox_1 = new JComboBox<String>();
		for (int i = 30; i < 121; i += 10) {
			comboBox_1.addItem(i + "분");
		}
		comboBox_1.setSelectedIndex(2);
		
		comboBox_2 = new JComboBox<String>();
		for (int i = 5; i < 31; i += 5) {
			comboBox_2.addItem(i + "분");
		}
		comboBox_2.setSelectedIndex(1);

		comboBox_3 = new JComboBox<String>();
		comboBox_3.addItem("없음");
		for (int i = 1; i < 8; i++) {
			comboBox_3.addItem(i + "교시 이후");
		}

		JButton button = new JButton("시작");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				startTime = System.currentTimeMillis();
				Timer timer = new Timer(MainSwing.this);
				jobScheduler.scheduleAtFixedRate(timer, 1, 1000);
			}
		});

		JButton btnNewButton = new JButton("정지");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				jobScheduler.cancel();
				label_2.setText("타이머시간 : 00:00:00");
			}
		});

		JButton btnNewButton_1 = new JButton("트레이");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new WavPlayer().start();
			}
		});
		GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addContainerGap()
							.addComponent(button)
							.addPreferredGap(ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
							.addComponent(btnNewButton)
							.addGap(22)
							.addComponent(btnNewButton_1))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(37)
							.addComponent(label_2))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(30)
							.addComponent(label_3))
						.addGroup(groupLayout.createSequentialGroup()
							.addContainerGap()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(label_1)
								.addComponent(label)
								.addComponent(lblNewLabel)
								.addComponent(lblNewLabel_1))
							.addGap(18)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(comboBox, Alignment.TRAILING, 0, 138, Short.MAX_VALUE)
								.addComponent(comboBox_2, 0, 138, Short.MAX_VALUE)
								.addComponent(comboBox_3, 0, 138, Short.MAX_VALUE)
								.addComponent(comboBox_1, Alignment.TRAILING, 0, 138, Short.MAX_VALUE))))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(20)
					.addComponent(label_3)
					.addGap(10)
					.addComponent(label_2)
					.addGap(30)
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(lblNewLabel)
						.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(lblNewLabel_1)
						.addComponent(comboBox_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(label)
						.addComponent(comboBox_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(label_1)
						.addComponent(comboBox_3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
							.addComponent(button)
							.addComponent(btnNewButton_1))
						.addComponent(btnNewButton))
					.addContainerGap(31, Short.MAX_VALUE))
		);
		frame.getContentPane().setLayout(groupLayout);
	}
}

class NowTime implements Runnable {
	MainSwing mainSwing;
	Calendar now;
	int year;
	int month;
	int day;
	int hour;
	int minute;
	int second;

	NowTime(MainSwing ms) {
		mainSwing = ms;
	}

	public void run() {
		String nowTimes = "";

		while (true) {
			now = Calendar.getInstance();
			SimpleDateFormat dateFormat = new SimpleDateFormat(
					"현재시간 : aa hh:mm:ss");
			nowTimes = dateFormat.format(now.getTime());
			mainSwing.label_3.setText(nowTimes);
			try {
				Thread.sleep(1000);
			} catch (Exception e) {
			}
		}
	}
}

class Timer extends TimerTask {
	SimpleDateFormat sdf;
	Timestamp timestamp;
	MainSwing mainSwing;
	long current;

	Timer(MainSwing ms) {
		mainSwing = ms;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while (true) {
			current = System.currentTimeMillis() - mainSwing.startTime
					+ 54000000L;
			timestamp = new Timestamp(current);
			sdf = new SimpleDateFormat("타이머시간 : HH:mm:ss");
			String nowTime = sdf.format(timestamp);
			mainSwing.label_2.setText(nowTime);
			timeCheck(timestamp);
			try {
				Thread.sleep(1000);
			} catch (Exception e) {
			}
		}
	}

	public void timeCheck(Timestamp timestamp) {
		String study = (String) mainSwing.comboBox_1.getSelectedItem();
		int studyTime = study.length() == 3 ? Integer.parseInt(study.substring(
				0, 2)) : Integer.parseInt(study.substring(0, 3));
		String rest = (String) mainSwing.comboBox_2.getSelectedItem();
		int restTime = rest.length() == 2 ? Integer.parseInt(rest.substring(0,
				1)) : Integer.parseInt(rest.substring(0, 2));
		if ((new SimpleDateFormat("ss").format(timestamp)).equals("00")) {
			int hour = Integer.parseInt(new SimpleDateFormat("HH")
					.format(timestamp));
			int minute = Integer.parseInt(new SimpleDateFormat("mm")
					.format(timestamp));
			minute = minute + hour * 60;
			boolean check=((minute%(studyTime+restTime))%studyTime==0)||
					((minute%(studyTime+restTime))==0);
			if (check) {
				new WavPlayer().start();
			}
		}
	}
}